<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="height: 3000px;">
  <div id="app">
    <button @click="isShow = !isShow">{{isShow?'消失':'显示'}}</button>
    <common-head v-if="isShow"></common-head>
  </div>
  <script src="./vue.js"></script>
  <script>
    const CommonHead = {
      template:`
        <div>
          <h1>这是一个组件</h1>
        </div>
      `,
      data(){
        return {
          timer: null
        }
      },
      mounted(){
        console.log('dom渲染完成');
        this.timer = setInterval(()=>{
          console.log(1111);
        },1000)
        window.onscroll = function(){
          var sTop = document.documentElement.scrollTop || document.body.scrollTop;
          console.log(sTop);
        }
      },
      beforeDestroy() {
        console.log('组件卸载之前');
        clearInterval(this.timer);
        window.onscroll = null;
      },
      destroyed() {
        console.log('组件已经卸载')
      },
    }
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })

    

  </script>
</body>
</html>